import {ReactiveEffect} from "./effect";

class ComputedRefImpl {
    private _getter: any;
    private _dirty: boolean = true;
    private _value: any;
    private _efftet: ReactiveEffect;

    constructor(getter) {
        this._getter = getter

        this._efftet = new ReactiveEffect(getter, () => {
            if (!this._dirty) {
                this._dirty = true;
            }
        });
    }

    get value() {
        if (this._dirty) {
            this._dirty = false
            //  shouldTrack = true;
            //         activeEffect = this;
            // 调用 ReactiveEffect 对象的 run 方法时 会给shouldTrack和activeEffect赋上值
            //  这样就可以做依赖收集了
            this._value = this._efftet.run()
        }
        return this._value;
    }


}

export function computed(getter) {
//     实现的思路是创建一个类,把getter函数当做一个属性传递给这个对象、对计算属性的访问都是对这个对象的value属性的get set 访问。
    return new ComputedRefImpl(getter);

}